import re
import json

import openpyxl


def award_prop(file_path):
    src_wb = openpyxl.load_workbook(src_path, data_only=True)
    src_ws = src_wb['平台API']

    names = [c.value for c in list(src_ws.columns)[0]][1:]
    props = [c.value for c in list(src_ws.columns)[1]][1:]
    values = [c.value for c in list(src_ws.columns)[2]][1:]

    d = {}
    for parentKey, childKey, value in zip(names, props, values):
        if not d.get(parentKey):
            d[parentKey] = {}
        d[parentKey][childKey] = value or 1

    json_str = json.dumps(d, ensure_ascii=False)
    json_str = re.sub(r'"(.+?)"', r'["\1"]', json_str)
    json_str = re.sub(r'}, \[', r'\n\t},\n\t[', json_str)
    json_str = re.sub(r': {\[', r' = {\n\t\t[', json_str)
    json_str = json_str.replace(':', ' =')
    json_str = re.sub(r'(\d+), \[', r'\1,\n\t\t[', json_str)
    json_str = re.sub(r'{', r'{\n\t', json_str, 1)
    json_str = re.sub(r'}}', r'\n\t}\n}', json_str)
    # print(json_str)

    file_path = r"D:\war3_workspace\heizhou\script\game\__config\awardCfg\propPrizeCfg.lua"
    with open(file_path, "w", encoding="utf-8") as f:
        f.write(f'local mt = {json_str}\n\nreturn mt\n')


def award_resource():
    pass


if __name__ == '__main__':
    src_path = input("工作簿路径：") or r'C:\Users\chenhao_wu\Desktop\黑咒3-发奖.xlsx'
    award_prop(src_path)
